library(dplyr)
library(ggplot2)
library(gridExtra)
library(lfe)
library(texreg)

setwd("D://Dropbox//Solidarity//replication//Data//")

women_cop_data <- read.csv("women_copartisan_donations.csv") %>%
  mutate(pair_id = paste0(receiver,year,matchid))

women_amount_cop_data <- read.csv("women_copartisan_donations_amount.csv") %>%
  mutate(pair_id = paste0(receiver,year,matchid))

# Appendix B: Figure 4
matchplot1 <- ggplot(women_cop_data, aes(x=num_other_recipients, linetype = gender)) + geom_density() + xlab("Number of Other Recipients") + ylab("Density") +
  labs(linetype = "Gender")
matchplot2 <- ggplot(women_cop_data, aes(x=nominate, linetype = gender)) + geom_density() + xlab("W-NOMINATE") + ylab("Density") +
  labs(linetype = "Gender")
grid.arrange(matchplot1, matchplot2, ncol = 2)

# Appendix B: Figure 5
matchplot3 <- ggplot(women_amount_cop_data, aes(x=log(other_giving + 1), linetype = gender)) + geom_density() + xlab("log(Total Given to Other Candidates+1)") + ylab("Density") +
  labs(linetype = "Gender")
matchplot4 <- ggplot(women_amount_cop_data, aes(x=nominate, linetype = gender)) + geom_density() + xlab("W-NOMINATE") + ylab("Density") +
  labs(linetype = "Gender")
grid.arrange(matchplot3, matchplot4, ncol = 2)

# Run regressions for Table 2
# Column 1
women.cop.fit <- felm(gave ~ I(gender == "F") + nominate + num_other_recipients + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_cop_data)
# Pull numbers for characterizing substantive significance of main effects
mean(women_cop_data$gave[which(women_cop_data$gender == "M")])
coef(women.cop.fit)[1]/mean(women_cop_data$gave[which(women_cop_data$gender == "M")])

# Column 2
women.amount.cop.fit <- felm(I(giving/1000) ~ I(gender == "F") + nominate + I(other_giving/1000) + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_amount_cop_data)
# Pull numbers for characterizing substantive significance of main effects
mean(women_amount_cop_data$giving[which(women_amount_cop_data$gender == "M")])
coef(women.amount.cop.fit)[1]*1000/mean(women_amount_cop_data$giving[which(women_amount_cop_data$gender == "M")])

# Columns 3-4
women.cop.civil.fit <- felm(gave ~ I(gender == "F") + civil_bills + I((gender == "F") * civil_bills) + nominate + num_other_recipients + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_cop_data)
women.amount.cop.civil.fit <- felm(I(giving/1000) ~ I(gender == "F") + civil_bills + I((gender == "F") * civil_bills)  + nominate + I(other_giving/1000) + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_amount_cop_data)

# Table 2
texreg(list(women.cop.fit, women.amount.cop.fit, women.cop.civil.fit, women.amount.cop.civil.fit),
       custom.model.names = c("Gave?", "Amount", "Gave?", "Amount"),
       custom.coef.names = c("Female Donor",
                             "W-NOMINATE",
                             "Other Campaigns Receiving from Donor",
                             "Same State as Recipient",
                             "Same Race as Recipient",
                             "Total Donor Gave to Other Candidates",
                             "Civil Rights Bills Sponsored",
                             "Female Donor $\\times$ Civil Rights Bills Sponsored"),
       caption = "Probability Incumbent Woman Gives to a Copartisan Woman Running Against a White Man",
       label = "tab:copfit",
       booktabs = TRUE,    # Optional: better LaTeX table styling
       dcolumn = TRUE,     # Align decimals if you use dcolumn in LaTeX
       use.packages = FALSE,  # Remove if you want texreg to suggest LaTeX packages
       digits = 3
       )

# Run analysis split by party
women.cop.dem.fit <- felm(gave ~ I(gender == "F") + nominate + num_other_recipients + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_cop_data, subset = which(party == "D"))
women.amount.cop.dem.fit <- felm(I(giving/1000) ~ I(gender == "F") + nominate + I(other_giving/1000) + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_amount_cop_data, subset = which(party == "D"))
women.cop.rep.fit <- felm(gave ~ I(gender == "F") + nominate + num_other_recipients + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_cop_data, subset = which(party == "R"))
women.amount.cop.rep.fit <- felm(I(giving/1000) ~ I(gender == "F") + nominate + I(other_giving/1000) + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_amount_cop_data, subset = which(party == "R"))
# Table 3
texreg(list(women.cop.dem.fit, women.amount.cop.dem.fit, women.cop.rep.fit, women.amount.cop.rep.fit),
       custom.model.names = c("Gave? (D)", "Amount (D)", "Gave? (R)", "Amount (R)"),
       custom.coef.names = c("Female Donor",
                             "W-NOMINATE",
                             "Other Campaigns Receiving from Donor",
                             "Same State as Recipient",
                             "Same Race as Recipient",
                             "Total Donor Gave to Other Candidates"),
       caption = "Probability Incumbent Woman Gives to a Copartisan Woman Running Against a White Man",
       label = "tab:partyfit",
       booktabs = TRUE,    # Better LaTeX table styling
       dcolumn = TRUE,     # Align decimals if using dcolumn in LaTeX
       use.packages = FALSE,  # Remove extra package suggestions in LaTeX
       digits = 3
)

# Run analysis split by challenger's incumbency status
women.cop.inc.fit <- felm(gave ~ I(gender == "F") + I((gender == "F") * (1-cop_incumbent)) + nominate + num_other_recipients + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_cop_data)
women.amount.cop.inc.fit <- felm(I(giving/1000) ~ I(gender == "F") + I((gender == "F") * (1-cop_incumbent)) + nominate + I(other_giving/1000) + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_amount_cop_data)
# Table 4
texreg(list(women.cop.inc.fit, women.amount.cop.inc.fit),
       custom.model.names = c("Gave?", "Amount"),
       custom.coef.names = c("Female Donor",
                             "Female Donor $\\times$ Challenger Recipient",
                             "W-NOMINATE",
                             "Other Campaigns Receiving from Donor",
                             "Same State as Recipient",
                             "Same Race as Recipient",
                             "Total Donor Gave to Other Candidates"),
       caption = "Probability Incumbent Woman Gives to a Copartisan Woman Running Against a White Man with Challenger Interaction",
       label = "tab:incfit",
       booktabs = TRUE,    # Optional: better LaTeX table styling
       dcolumn = TRUE,     # Align decimals if using dcolumn package
       use.packages = FALSE,  # Don’t add extra LaTeX package suggestions
       digits = 3
)

# Run analysis with leadership interactions
women.leader.cop.fit <- felm(gave ~ I(gender == "F") + I(gender == "F" & terminal_pos) + I(gender == "F" & mezz_pos) + terminal_pos + mezz_pos + nominate + num_other_recipients + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_cop_data)
women.leader.amount.cop.fit <- felm(I(giving/1000) ~ I(gender == "F") + I(gender == "F" & terminal_pos) + I(gender == "F" & mezz_pos) + terminal_pos + mezz_pos + nominate + I(other_giving/1000) + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_amount_cop_data)
# Table 5
texreg(list(women.leader.cop.fit, women.leader.amount.cop.fit), digits = 3)

# Run analysis with mentor interaction
women.mentor.cop.fit <- felm(gave ~ I(gender == "F") + I(gender == "F" & mentor) + nominate + num_other_recipients + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_cop_data)
women.mentor.amount.cop.fit <- felm(I(giving/1000) ~ I(gender == "F") + I(gender == "F" & mentor) + nominate + I(other_giving/1000) + same_state + I(race == cand_race) | interaction(receiver, year, matchid) | 0 | giver, data = women_amount_cop_data)
# Table 6
texreg(list(women.mentor.cop.fit, women.mentor.amount.cop.fit), digits = 3)

# Load data for minority analysis in Appendix C
poc_cop_data <- read.csv("poc_copartisan_donations.csv") %>%
  mutate(pair_id = paste0(receiver,year,matchid))
poc_amount_cop_data <- read.csv("poc_copartisan_donations_amount.csv") %>%
  mutate(pair_id = paste0(receiver,year,matchid))

# Run regressions for Table 11
poc.cop.fit <- felm(gave ~ I(race != "White") + nominate + num_other_recipients + same_state + I(gender == cand_gender) | interaction(receiver, year, matchid) | 0 | giver, data = poc_cop_data)
poc.amount.cop.fit <- felm(I(giving/1000) ~ I(race != "White") + nominate + I(other_giving/1000) + same_state + I(gender == cand_gender) | interaction(receiver, year, matchid) | 0 | giver, data = poc_amount_cop_data)
poc.cop.civil.fit <- felm(gave ~ I(race != "White") + civil_bills + I((race != "White") * civil_bills) + nominate + num_other_recipients + same_state + I(gender == cand_gender) | interaction(receiver, year, matchid) | 0 | giver, data = poc_cop_data)
poc.amount.cop.civil.fit <- felm(I(giving/1000) ~ I(race != "White") + civil_bills + I((race != "White") * civil_bills) + nominate + I(other_giving/1000) + same_state + I(gender == cand_gender) | interaction(receiver, year, matchid) | 0 | giver, data = poc_amount_cop_data)

# Table 11
texreg(list(poc.cop.fit, poc.amount.cop.fit, poc.cop.civil.fit, poc.amount.cop.civil.fit),
       custom.model.names = c("Gave?", "Amount", "Gave?", "Amount"),
       custom.coef.names = c("Minority Donor",
                             "W-NOMINATE",
                             "Other Campaigns Receiving from Donor",
                             "Same State as Recipient",
                             "Same Race as Recipient",
                             "Total Donor Gave to Other Candidates",
                             "Civil Rights Bills Sponsored",
                             "Minority Donor $\\times$ Civil Rights Bills Sponsored"),
       caption = "Probability Incumbent Woman Gives to a Copartisan Woman Running Against a White Man",
       label = "tab:poccopfit",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       digits = 3
)


# Run regressions for Table 12
poc.cop.inc.fit <- felm(gave ~ I(race != "White") + I((race != "White") * (1-cop_incumbent)) + nominate + num_other_recipients + same_state + I(gender == cand_gender) | interaction(receiver, year, matchid) | 0 | giver, data = poc_cop_data)
poc.amount.cop.inc.fit <- felm(I(giving/1000) ~ I(race != "White") + I((race != "White") * (1-cop_incumbent)) + nominate + I(other_giving/1000) + same_state + I(gender == cand_gender) | interaction(receiver, year, matchid) | 0 | giver, data = poc_amount_cop_data)

# Table 12
texreg(list(poc.cop.inc.fit, poc.amount.cop.inc.fit),
       custom.model.names = c("Gave?", "Amount"),
       custom.coef.names = c("Minority Donor",
                             "Minority Donor $\\times$ Challenger Recipient",
                             "W-NOMINATE",
                             "Other Campaigns Receiving from Donor",
                             "Same State as Recipient",
                             "Same Race as Recipient",
                             "Total Donor Gave to Other Candidates"),
       caption = "Probability Incumbent Woman Gives to a Copartisan Woman Running Against a White Man with Challenger Interaction",
       label = "tab:pocincfit",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       digits = 3
)

# Load data for intersectional analysis
intersectional_cop_data <- read.csv("intersectional_copartisan_donations.csv")
intersectional_amount_cop_data <- read.csv("intersectional_copartisan_donations_amount.csv") 

# Run regressions for Table 13
intersectional.cop.fit <- felm(gave ~ I(race == "White") + I(gender == "M") + nominate + num_other_recipients + same_state | interaction(receiver, year, matchid) | 0 | giver, data = intersectional_cop_data)
intersectional.amount.cop.fit <- felm(I(giving/1000) ~ I(race == "White") + I(gender == "M") + nominate + I(other_giving/1000) + same_state | interaction(receiver, year, matchid) | 0 | giver, data = intersectional_amount_cop_data)
intersectional.cop.civil.fit <- felm(gave ~ I(race == "White") + I(gender == "M") + civil_bills + I((race == "White") * civil_bills) + I((gender == "M")*civil_bills) + nominate + num_other_recipients + same_state | interaction(receiver, year, matchid) | 0 | giver, data = intersectional_cop_data)
intersectional.amount.cop.civil.fit <- felm(I(giving/1000) ~ I(race == "White") + I(gender == "M") + civil_bills + I((race == "White") * civil_bills) + I((gender == "M")*civil_bills) + nominate + I(other_giving/1000) + same_state | interaction(receiver, year, matchid) | 0 | giver, data = intersectional_amount_cop_data)

# Table 13
texreg(list(intersectional.cop.fit, intersectional.amount.cop.fit, intersectional.cop.civil.fit, intersectional.amount.cop.civil.fit),
       custom.model.names = c("Gave?", "Amount", "Gave?", "Amount"),
       custom.coef.names = c("White Female Donor",
                             "Minority Male Donor",
                             "W-NOMINATE",
                             "Other Campaigns Receiving from Donor",
                             "Total Donor Gave to Other Candidates",
                             "Same State as Recipient",
                             "Civil Rights Bills Sponsored",
                             "White Female Donor $\\times$ Civil Rights Bills Sponsored",
                             "Male Minority Donor $\\times$ Civil Rights Bills Sponsored"),
       caption = "Giving Compared to Minority Female Donors",
       label = "tab:intersectionalcopfit",
       booktabs = TRUE,    # For LaTeX styling
       dcolumn = TRUE,     # Align decimals if using dcolumn
       use.packages = FALSE, # Avoid extra package suggestions
       digits = 3
)